package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.rfid.base.base.BaseListFragment;
import com.rfid.base.base.BasePresenter;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.SelectAdapter;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.manager.FragmentDataManager;
import com.rfid.emergencywarehouse.reader.MDividerItemDecoration;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectFragment extends BaseListFragment<SelectAdapter,NameValueBean, BasePresenter> implements FragmentDataManager.Listener {
    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_select;
    }

    @Override
    protected SelectAdapter getAdapter() {
        return new SelectAdapter(mData);
    }

    @Override
    protected int getRecyclerViewId() {
        return R.id.recyclerView;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    NameValueBean mSelectValue;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        FragmentDataManager.INSTANCE.addListener(this);
        showRightView(true);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        FragmentDataManager.INSTANCE.removeListener(this);
    }

    @Override
    public void onDatasChange(List<NameValueBean> beans) {
        setData(beans);
    }

    @OnClick(R.id.right_view)
    public void confirm(View view) {
        FragmentDataManager.INSTANCE.select(mSelectValue);
        getActivity().onBackPressed();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MDividerItemDecoration itemDecoration = new MDividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);
        if (FragmentDataManager.INSTANCE.getData() != null) {
            setData(FragmentDataManager.INSTANCE.getData());
        }
        ButterKnife.bind(this,view);
    }

    @Override
    public void setData(List<NameValueBean> books) {
        super.setData(books);
        if (mData != null && mData.size() > 0) {
            mSelectValue = mData.get(0);
        }
    }

    @Override
    public void onClick(NameValueBean bean) {
        mSelectValue = bean;
    }

}
